package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MenuMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.pojo.*;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Integer> getMenus(Integer roleId) {
        return roleMapper.getMenus(roleId);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    /**
     * 分页查询角色
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        //定义当前页的页码和页面大小
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //条件查询所有数据
        List<Role> roles = roleMapper.findByCondition(queryPageBean.getQueryString());
        //将查询出来的数据包装
        PageInfo<Role> pageInfo = new PageInfo<>(roles);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }
    /**
     * @title add
     * @description 新增角色
     * @author juju
     * @updateTime 2020/6/29 20:30
     * @throws
     */
    @Override
    public void add(Integer[] keywordIds,Integer[] menuIds, Role role) {
//        向角色表中添加角色
        roleMapper.add(role);
//        新增角色和权限的中间表数据
        setRoleAndPermission(role.getId(),keywordIds);
//        新增角色和菜单的中间表关系
        setRoleAndMenu(role.getId(),menuIds);
    }
    /**
     * @title findPermission
     * @description 查询所有权限
     * @author juju
     * @updateTime 2020/6/29 23:34
     * @throws
     */
    @Override
    public List<Permission> findPermission() {
        return roleMapper.findPermission();
    }
    /**
     * @title findMenu
     * @description 查询所有菜单
     * @author juju
     * @updateTime 2020/6/29 23:35
     * @throws
     */
    @Override
    public List<Menu> findMenu() {
        return roleMapper.findMenu();
    }
    /**
     * @title findPermissionIdsByRoleId
     * @description 根据角色id查询其权限id
     * @author juju
     * @updateTime 2020/6/30 9:27
     * @throws
     */
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleMapper.findPermissionIdsByRoleId(id);
    }
    /**
     * @title findMenuIdsByRoleId
     * @description 根据角色id查询其菜单id
     * @author juju
     * @updateTime 2020/6/30 9:27
     * @throws
     */
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleMapper.findMenuIdsByRoleId(id) ;
    }
    /**
     * @title edit
     * @description 编辑角色
     * @author juju
     * @updateTime 2020/6/30 9:59
     * @throws
     */
    @Override
    public void edit(Integer[] keywordIds, Integer[] menuIds, Role role) {
//        将角色和权限的中间表删除
        roleMapper.deleteAssociationWithPermission(role.getId());
//        新增角色和权限的中间表数据
        setRoleAndPermission(role.getId(),keywordIds);
//        将角色和菜单的中间表删除
        roleMapper.deleteAssociationWithMenu(role.getId());
//        新增角色和菜单的中间表关系
        setRoleAndMenu(role.getId(),menuIds);
//        更新角色
        roleMapper.edit(role);
    }
    /**
     * @title deleteById
     * @description 根据角色的id删除角色
     * @author juju
     * @updateTime 2020/6/30 11:05
     * @throws
     */
    @Override
    public void deleteById(Integer id) {
//根据角色id删除与用户之间的关系
//        先判断该角色是否被用户引用
        Integer count=roleMapper.checkUserAndRole(id);
        if (count > 0) {
//            当前的角色被引用不能被删除
            throw new RuntimeException(MessageConstant.ROLE_IS_QUOTED);
        } else {
//            与用户没有关联则依次删除
//        根据角色删除与权限的关系
            roleMapper.deleteAssociationWithPermission(id);
//        根据角色删除与菜单的关系
            roleMapper.deleteAssociationWithMenu(id);
//        删除角色
            roleMapper.deleteRole(id);
        }
    }

    private void setRoleAndMenu(Integer id,Integer[] menuIds) {
        if (menuIds.length > 0 && id != null) {
            roleMapper.setRoleAndMenu(id,menuIds);
        }
    }

    private void setRoleAndPermission(Integer id,Integer[] keywordIds ) {
        if (keywordIds.length > 0 && id!= null) {
            roleMapper.setRoleAndPermission(id,keywordIds);
        }
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return roleMapper.findRoleIdsByUserId(id);
    }

}
